package com.kwshare.common.security.service;

import com.kwshare.common.core.constant.SecurityConstants;
import com.kwshare.common.core.constant.TokenConstants;
import com.kwshare.common.core.domain.auth.LoginUser;
import com.kwshare.common.core.utils.JwtUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * token验证处理
 *
 * @author yangjing
 */
@Component
public class TokenService {

    /**
     * 创建令牌
     *
     * @param loginUser 用户信息
     * @return 结果
     */
    public Map<String, Object> createToken(LoginUser loginUser){
        // 构建Jwt Payload
        Map<String, Object> claimsMap = new HashMap<>();
        claimsMap.put(SecurityConstants.DETAILS_USERNAME,loginUser.getUser().getUsername());
        // 生成token
        String token = JwtUtils.createToken(claimsMap);
        // 返回token信息
        Map<String, Object> rspMap = new HashMap<String, Object>();
        rspMap.put(TokenConstants.ACCESS_TOKEN,token);
        rspMap.put(TokenConstants.EXPIRES_IN,JwtUtils.getExpiration(token));
        return rspMap;
    }
}
